package com.polaris.live.im.sfs.processor.impl

import com.blankj.utilcode.util.ActivityUtils
import com.polaris.live.common.manager.CoroutineScopeManager
import com.polaris.live.common.util.JsonUtils
import com.polaris.live.constant.LiveConst
import com.polaris.live.im.sfs.COMMAND_INVITE_MATCH
import com.polaris.live.im.sfs.bean.pusher.NoticeMatchMessage
import com.polaris.live.im.sfs.bean.pusher.PusherNoticeMessage
import com.polaris.live.im.sfs.core.bean.DataArgument
import com.polaris.live.im.sfs.processor.AbstractMessageProcessor
import com.polaris.live.manager.LiveStateManager
import com.polaris.live.utils.route.RoutePath
import com.polaris.live.utils.route.Router
import kotlinx.coroutines.withContext

/**
 * 新人视频邀约
 *
 * @author Created by lucas on 2023/10/23 15:32
 * @since 1.0.0
 */
class NoticeInviteMessageProcessor : AbstractMessageProcessor() {

    override fun shouldProcess(type: Int, command: String): Boolean {
        return command == COMMAND_INVITE_MATCH
    }

    override suspend fun handle(type: Int, command: String, argument: DataArgument) {
        val data = argument.getData<PusherNoticeMessage>() ?: return
        val message = JsonUtils.fromJson<NoticeMatchMessage>(data.content) ?: return

        withContext(CoroutineScopeManager.mainDispatcher) {
            if (LiveStateManager.isNone()) {
                Router.builder(RoutePath.NOTICE_MATCH_ACTIVITY)
                    .withAny(LiveConst.Params.NOTICE_MATCH_MESSAGE, message)
                    .navigation(ActivityUtils.getTopActivity())
            }
        }
    }
}